package menu;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.IOException;
import javax.swing.JButton;
import javax.swing.SwingConstants;
import utility.MP3Player;
import menu.PanelMain.ButtonAction;

public class ButtonMenu extends JButton implements MouseListener{
	private static final long serialVersionUID = 1L;
	private ButtonAction action;
	private Font f;
	private MP3Player click = new MP3Player("src/sound/beep.mp3");
	
	public ButtonMenu(String name) throws IOException
	{
		super.setName(name);
		this.setName(name);
		
		f = new Font("IMPACT", Font.PLAIN, 20);
		
		this.setForeground(Color.orange);
		this.setFont(f);
		this.setBorderPainted(false);
		this.setHorizontalTextPosition(SwingConstants.CENTER);
		this.setVerticalTextPosition(SwingConstants.CENTER);
		this.setText(name);
		this.setBorder(null);
		this.setContentAreaFilled(false);
		this.addMouseListener(this);
		this.setDoubleBuffered(true);
		this.setIcon(null);
		this.setName(name);
		this.setOpaque(false);
		this.setMinimumSize(new Dimension(100, 30));
		this.setPreferredSize(new Dimension(100, 30));
		this.setVisible(true);
	}
	
	@Override
	public void mouseClicked(MouseEvent e) {
		if (e.getButton()==MouseEvent.BUTTON1)
		{
			try {
				((PanelMain)this.getParent().getParent()).doAction(action);
			} catch (IOException e1) {
				e1.printStackTrace();
			}
		}
	}

	@Override
	public void mouseEntered(MouseEvent e) {
		this.setForeground(Color.yellow);
		click.play();
	}

	@Override
	public void mouseExited(MouseEvent e) {
		this.setForeground(Color.orange);
	}

	@Override
	public void mousePressed(MouseEvent e) {
		this.setForeground(Color.red);
	}

	@Override
	public void mouseReleased(MouseEvent e) {
		this.setForeground(Color.orange);
	}
	
	public ButtonAction getButtonAction() {
		return action;
	}

	public void setAction(ButtonAction action) {
		this.action = action;
	}
	
	@Override
	protected void paintComponent(Graphics g) {
		super.paintComponent(g);
		int x1 = 0;
		int x2 = (int) this.getSize().getWidth();
		int y1 = 3;
		int y2 = (int) this.getSize().getHeight()-3;
		
		this.getGraphics().setColor(Color.yellow);
		this.getGraphics().drawLine(x1, y1, x2, y1);
		this.getGraphics().drawLine(x1, y2, x2, y2);
	}
}
